﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace VirtualDisk
{
    class DelCommand : Command
    {
        private MyString[] arguments = null;


        protected override int run()
        {
            if (arguments == null || arguments.Length == 0)
            {
                return 1;
            }

            MyString path = arguments[0].Trim();
            if (!DiskTree.getInstance().isExist(path))
            {
                return 2;
            }
            MyString name = DiskHelper.getFileName(path);
            if (name.IndexOfAny(new char[] { '?', '*' }) != -1)
            {
                //含有通配符
                DiskTree.getInstance().removeNodeByWildcard(path);
            }
            else
            {
                //未含有通配符
                if (DiskTree.getInstance().isADirctory(path))
                {
                    return 3;
                }
                DiskTree.getInstance().removeNode(path);
            }

            return 0;
        }

        protected override void processError(int returnCode)
        {
            switch (returnCode)
            {
                case 1:
                    Console.WriteLine("命令语法不正确");
                    break;
                case 2:
                    Console.WriteLine("找不到" + arguments[0]);
                    break;
                case 3:
                    Console.WriteLine("无法删除目录" + arguments[0]);
                    break;
                default:
                    break;

            }
        }

        public DelCommand(MyString[] argu)
        {
            if (argu != null)
            {
                arguments = new MyString[argu.Length];
                for (int i = 0; i < arguments.Length; i++)
                {
                    arguments[i] = argu[i];
                }
            }

        }
    }
}
